package com.ly.mpayment.rest.system;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import com.ly.mpayment.core.util.*;
import com.ly.mpayment.persist.entity.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.persist.service.zero.ZeroBalanceService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Component
@Path("system/zeroBalance/")
public class ZeroBalanceRest extends AbsRest {

	@Autowired
	IUserService userService;

	@Autowired
	ZeroBalanceService zeroBalanceService;


	@POST
	@Path("find")
	public String searchUserLog(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,"start","pageSize","createtime","endtime","userId");
		if(result == null){
			String token = request.getHeader("token");
			JSONObject gson = JsonUtils.converToJson(data);
			JSONObject json = JsonUtils.converToJson(JWT.decode(token).getSubject());
			User user = this.userService.findByUserid(json.getString(Config.tokenSignAttr));
			if(user != null){
				Map<String, Object> parameter = MapUtil.getMap();
				if(!StringUtils.isBlank(gson.getString("createtime"))){
					parameter.put(":createtime", gson.getString("createtime"));
				}
				if(!StringUtils.isBlank(gson.getString("endtime"))){
					parameter.put(":endtime", gson.getString("endtime"));
				}
				if(!StringUtils.isBlank(gson.getString("userId"))){
					parameter.put(":userId", gson.getString("userId"));
				}

				if(user.getUsertype() == 3) {
					parameter.put(":userId", user.getUseruuid());
					return ResultMessage.getSucceed( JsonUtils.excludes(this.zeroBalanceService.pageZero(parameter, gson.getInt("start"),  gson.getInt("pageSize"))
							, new String[]{"phonenum","password","userstatus","usertype","token","lastlogintime","wechatid","expire","errorCodesForUseruuid"}));
				}else if(user.getUsertype() == 1 || user.getUsertype() == 2) {
					return ResultMessage.getSucceed( JsonUtils.excludes(this.zeroBalanceService.pageZero(parameter, gson.getInt("start"),  gson.getInt("pageSize"))
							, new String[]{"phonenum","password","userstatus","usertype","token","lastlogintime","wechatid","expire","errorCodesForUseruuid"}));
				}
			}
			return ResultMessage.getFailt(ResultMessage.tokenError);
		}
		return result;
	}


}
